/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TextDisplay.java
 *
 * Created on 09-Feb-2009, 23:36:21
 */

package bluetoothserver.graphical;

import bluetoothserver.dataStore.AnswerCollection;
import bluetoothserver.dataStore.AnswerUnits;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Philip
 */
public class TextDisplay extends javax.swing.JPanel {

    private Random random ;
    private AnswerCollection ans;
    private ArrayList<AnswerUnits> words;
    private int width, height;
    /** Creates new form TextDisplay */

    public TextDisplay(int w,int h,ArrayList<AnswerUnits> wd) {
        width = w;
        height = h;
        words = wd;
        random = new Random();
        ans = AnswerCollection.getInstance();
        initComponents();
        this.setVisible(true);
    }

    private void addWord(String word ,int state,Graphics g){
        int x = this.getRandom();
        int y = this.getRandom();
        AttributedString as = new AttributedString(word);
        if(state < 4){
            // Any words that have less than 5 people voted
            Font f = new Font("Monospaced",Font.PLAIN,16);

            as.addAttribute(TextAttribute.FONT, f);
            as.addAttribute(TextAttribute.FOREGROUND, Color.PINK);
        }else if(state < 9){
            // Any words that have between 6 - 10 people voted
            Font f = new Font("Monospaced",Font.PLAIN,32);
            as.addAttribute(TextAttribute.FONT, f);
            as.addAttribute(TextAttribute.FOREGROUND, Color.orange);
        }else if(state < 14){
            // Any words that have between 11 - 15 people voted
            Font f = new Font("Monospaced",Font.PLAIN,64);
            as.addAttribute(TextAttribute.FONT, f);
            as.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
        }else if(state < 19){
            // Any words that have between 16 - 20 people voted
            Font f = new Font("Monospaced",Font.PLAIN,128);
            as.addAttribute(TextAttribute.FONT, f);
            as.addAttribute(TextAttribute.FOREGROUND, Color.RED);
        }else{
            //Any word that have between
            Font f = new Font("Monospaced",Font.PLAIN,256);
            as.addAttribute(TextAttribute.FONT, f);
            as.addAttribute(TextAttribute.FOREGROUND, Color.BLUE);
        }
        //Draws the string on the display
        g.drawString(as.getIterator(), x, y);

    }

    @Override
    public void paint(Graphics g){
//        g.setColor(Color.BLACK);
//        g.fillRect(0, 0, width, height);
        String s = ans.getAnswers(0).getTitle();
        AttributedString as = new AttributedString(s);
        Font f = new Font("Monospaced",Font.PLAIN,32);
        as.addAttribute(TextAttribute.FONT, f);
        as.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
        FontMetrics fm = g.getFontMetrics(f);
        int pos = fm.stringWidth(s)/2;
        int centre = width/2;
        g.drawString(as.getIterator(),centre-pos,45);
        for(int i = 0;i < words.size();i++){
            this.addWord(words.get(i).getAnswerName(), words.get(i).getNumberOfVotes(), g);
        }

    }
    
    public int getRandom(){
        int i = random.nextInt(800);
        if(i < 50){
            i = getRandom();
        }
        return i;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 733, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 530, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}